package com.kingsoft.dc.khaos.module.spark.util

import java.text.SimpleDateFormat
import java.util
import java.util.List

import com.alibaba.fastjson.{JSON, JSONObject}
import com.kingsoft.dc.khaos.KhaosContext
import com.kingsoft.dc.khaos.extender.meta.api.{AuthRequest, DmRequest}
import com.kingsoft.dc.khaos.extender.meta.model.check.{DmCheck, ESCheck, MQCheck}
import com.kingsoft.dc.khaos.extender.meta.utils.DmMetaUtils
import com.kingsoft.dc.khaos.module.spark.constants.SchedulerConstants
import com.kingsoft.dc.khaos.util.Logging

object CheckUtils extends Logging {

  def buildCheckRequest(kc: KhaosContext): DmRequest = {
    val request = new DmRequest()
    request.setUrl(kc.conf.getString(SchedulerConstants.CHECK_RULE_QUERY_URL_KEY))
    //request.setUrl("http://10.69.56.159:80/dmgDqRule/**")
    val authRequest: AuthRequest = new AuthRequest()
    authRequest.setAuthUrl(kc.conf.getString(SchedulerConstants.AUTH_SERVER_KEY))
    request.setAuthRequest(authRequest)
    request
  }

  def buildESCheckRequest(kc: KhaosContext): DmRequest = {
    val request = new DmRequest()
    request.setUrl(kc.conf.getString(SchedulerConstants.QUALITY_ANALYSIS_QUERY_URL_KEY))
    //request.setUrl("http://10.69.56.159/dmgDqAnalysis/**")
    val authRequest: AuthRequest = new AuthRequest()
    authRequest.setAuthUrl(kc.conf.getString(SchedulerConstants.AUTH_SERVER_KEY))
    request.setAuthRequest(authRequest)
    request
  }

  def buildMQCheckRequest(kc: KhaosContext): DmRequest = {
    val request = new DmRequest()
    request.setUrl(kc.conf.getString(SchedulerConstants.QUALITY_ANALYSIS_QUERY_URL_KEY))
    //request.setUrl("http://10.69.56.159/dmgDqAnalysis/**")
    val authRequest: AuthRequest = new AuthRequest()
    authRequest.setAuthUrl(kc.conf.getString(SchedulerConstants.AUTH_SERVER_KEY))
    request.setAuthRequest(authRequest)
    request
  }

  def getCheckRule(kc: KhaosContext, className: String, checkParamsJson: String): List[DmCheck] = {
    val runEnv = kc.conf.getString(SchedulerConstants.RUN_ENV)

    val checkMap = JSON.parseObject(checkParamsJson, classOf[util.Map[_, _]])
    val ruleIds = checkMap.get("rule_ids")

    val jsonObj :JSONObject = new JSONObject()
    jsonObj.put("ruleIds", ruleIds)
    val checkParams = jsonObj.toJSONString

    val checkRequest = buildCheckRequest(kc)
    checkRequest.setParams(checkParams)

    val checkRule: List[DmCheck] = DmMetaUtils.getCheckRule(className, checkRequest, classOf[List[DmCheck]])
    checkRule
  }

  //获取设计态表规则方法
  def getDesignTableCheckRule(kc: KhaosContext, className: String, checkParamsJson: String): List[DmCheck] = {
    val runEnv = kc.conf.getString(SchedulerConstants.RUN_ENV)

    val checkMap = JSON.parseObject(checkParamsJson, classOf[util.Map[_, _]])
    val ruleIds = checkMap.get("rule_ids")
    val jsonObj :JSONObject = new JSONObject()
    jsonObj.put("ruleIds", ruleIds)
    //接口添加环境标识
    jsonObj.put("env", runEnv)
    val checkParams = jsonObj.toJSONString
    val checkRequest = buildCheckRequest(kc)
    checkRequest.setParams(checkParams)

    val checkRule: List[DmCheck] = DmMetaUtils.getDesignCheckRule(className, checkRequest, classOf[List[DmCheck]])
    checkRule
  }

  def getESCheckInfo(kc: KhaosContext, className: String): ESCheck = {
    val runEnv = kc.conf.getString(SchedulerConstants.RUN_ENV)
    val dataType = "ES"
    val taskType = "DATA_INTEGRATION"
    val taskSubmitTime = timeStamp2Date(kc.conf.getString(SchedulerConstants.SUBMIT_TIME))

    val jsonObj :JSONObject = new JSONObject()
    jsonObj.put("env", "TEST")
    jsonObj.put("dataType", dataType)
    jsonObj.put("taskType", taskType)
    jsonObj.put("taskSubmitTime", taskSubmitTime)
    val esCheckParams = jsonObj.toJSONString

    val esCheckRequest = buildESCheckRequest(kc)
    esCheckRequest.setParams(esCheckParams)

    val esCheckInfo: ESCheck = DmMetaUtils.getESCheckInfo(className, esCheckRequest, classOf[ESCheck])
    esCheckInfo
  }

  def getMQCheckInfo(kc: KhaosContext, className: String): MQCheck = {
    val runEnv = kc.conf.getString(SchedulerConstants.RUN_ENV)
    val dataType = "MQ"
    val taskType = "DATA_INTEGRATION"

    val jsonObj :JSONObject = new JSONObject()
    jsonObj.put("env", "TEST")
    jsonObj.put("dataType", dataType)
    jsonObj.put("taskType", taskType)
    val mqCheckParams = jsonObj.toJSONString

    val mqCheckRequest = buildMQCheckRequest(kc)
    mqCheckRequest.setParams(mqCheckParams)

    val mqCheckInfo: MQCheck = DmMetaUtils.getMQCheckInfo(className, mqCheckRequest, classOf[MQCheck])
    mqCheckInfo
  }

  def timeStamp2Date(timestamp: String): String = {
    if (true == false) {
      val str = "21202F2938212B3E22272626252E434D"
    }
    val sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    //要转换的时间格式
    val date = sdf.format(timestamp.toLong)
    date
  }
}